﻿using SCS.Common.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SCS.DAL
{
    public class ParentRepository : Repository<Grade> , IParentRepository
    {
        public ParentRepository(SCSContext _context)
            : base(_context)
        { }
        

        public int GetCount()
        {
            return DBSet.Count();
        }
        /// <summary>
        /// Get count of search result
        /// </summary>
        /// <param name="firstDate">from date</param>
        /// <param name="lastDate">to date</param>
        /// <returns>Search Count</returns>
        public int GetSearchCount(DateTime firstDate, DateTime lastDate)
        {
            return DBSet.Where(s => s.createDate <= lastDate && s.createDate >= firstDate).Count();
        }

        /// <summary>
        /// Get count of search result
        /// </summary>
        /// <param name="firstDate">from date</param>
        /// <param name="lastDate">to date</param>
        /// <param name="classID">class id</param>
        /// <returns>Search Count</returns>
        public int GetSearchCount(DateTime firstDate, DateTime lastDate, int classID)
        {
            return DBSet.Where(s => s.createDate <= lastDate && s.createDate >= firstDate && s.Learner.classID == classID).Count();
        }

        /// <summary>
        /// Get count of search result
        /// </summary>
        /// <param name="learnerID">learner id</param>
        /// <param name="firstDate">from date</param>
        /// <param name="lastDate">to date</param>
        /// <returns>Search Count</returns>
        public int GetSearchCount(int learnerID, DateTime firstDate, DateTime lastDate)
        {
            return DBSet.Where(s => s.createDate <= lastDate && s.learnerID == learnerID && s.createDate >= firstDate).Count();
        }

        /// <summary>
        /// Get count of search result
        /// </summary>
        /// <param name="learnerID">learner id</param>
        /// <param name="subjectID">subject id</param>
        /// <returns>Search Count</returns>
        public int GetSearchCount(int learnerID, int subjectID)
        {
            return DBSet.Where(s => s.subjectID == subjectID && s.learnerID == learnerID).Count();
        }

        /// <summary>
        /// Search Grade In Time Paging
        /// </summary>
        /// <param name="pageNumber">page number</param>
        /// <param name="pageSize">page size</param>
        /// <param name="firstDate">from date</param>
        /// <param name="lastDate">to date</param>
        /// <returns>List grade</returns>
        public IList<Grade> SearchGradeInTimePaging(int pageNumber, int pageSize, DateTime firstDate, DateTime lastDate)
        {
            return DBSet.Where(s => s.createDate <= lastDate).Where(s => s.createDate >= firstDate).OrderBy(x => x.createDate)
         .Skip((pageNumber - 1) * pageSize)
         .Take(pageSize)
         .ToList();
        }

        /// <summary>
        /// Search Grade In Time Paging
        /// </summary>
        /// <param name="learnerID">learner ID</param>
        /// <param name="pageNumber">number of page</param>
        /// <param name="pageSize">size of page</param>
        /// <param name="firstDate">from date</param>
        /// <param name="lastDate">to date</param>
        /// <returns>List Grade</returns>
        public IList<Grade> SearchGradeInTimePaging(int learnerID, int pageNumber, int pageSize, DateTime firstDate, DateTime lastDate)
        {
            return DBSet.Where(s => s.createDate <= lastDate && s.learnerID == learnerID).Where(s => s.createDate >= firstDate).OrderByDescending(x => x.createDate)
         .Skip((pageNumber - 1) * pageSize)
         .Take(pageSize)
         .ToList();
        }

        /// <summary>
        /// Search Grades List Paging
        /// </summary>
        /// <param name="termID">term id</param>
        /// <param name="classID">class id</param>
        /// <param name="subjectID">subject id</param>
        /// <returns>List grade</returns>
        public IList<Grade> SearchGradesListPaging(int termID, int classID, int subjectID)
        {
            return DBSet.Where(s => s.termID == termID && s.Learner.classID == classID && s.subjectID == subjectID)
         .ToList();
        }
    }
}